home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / paintCreateFunc.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  8.2 KB  |  227 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. // Alias|Wavefront Script File
  18. // MODIFY THIS AT YOUR OWN RISK
  19. //
  20. // paintCreateFunc
  21. //
  22. // This is provided to users as a template for creating
  23. // custom tube generation for paint effects brushes.
  24. // If one toggles on "Tubes" and types the name of this
  25. // script into the field "creation script"(under User Mel Scripts)
  26. // then this mel function overrides the normal tube creation
  27. // routine(C code) for that brush.
  28. // Maya assumes that this function has the exact arguments
  29. // listed here. The newly created tubes ( a bit like emitted particles )
  30. // are passed back to maya by returning an array of values.
  31. // The first element in this array is the number of new tubes, and
  32. // the rest of the array is a set of parameters that define each tube.
  33. // This template makes use of the passed in stroke segment and normal
  34. // to orient the created tubes along the brushstoke.
  35.  
  36. global proc float []
  37. paintCreateFunc(
  38.  
  39.     // current segment count on base brushstroke
  40. int      $step,
  41.  
  42.     // start stroke segment in worldspace
  43. float $strokeX1, float $strokeY1, float $strokeZ1,
  44.  
  45.     // end stroke segment in worldspace
  46. float $strokeX2, float $strokeY2, float $strokeZ2,
  47.  
  48.     // current surface normal
  49. float $nX, float $nY, float $nZ,
  50.  
  51.     // a random seed that is unique for this stroke segment
  52. int   $randSeed,
  53.  
  54.     // The following parameters are attributes directly from the brush node
  55.     // Not all parameters have been provided for brevity and efficiency.
  56.     // TubesPerStep and segments may be affected by display quality when
  57.     // this function is called during a wireframe redraw
  58. float $width,     // worldspace brush width
  59. float $tubesPerStep, // requested number of new tubes for this step
  60. int      $maxSegments, // max number of segments in a tube 
  61. float $tubeWidth1, 
  62. float $tubeWidth2, 
  63. float $elevationMax, 
  64. float $azimuthMax, 
  65. float $lengthMax, 
  66. // start twist value 
  67. float $twist,
  68. float $spiralMax, 
  69. float $color1R, float $color1G, float $color1B,
  70. float $color2R, float $color2G, float $color2B,
  71. float $transparency1R, float $transparency1G, float $transparency1B,
  72. float $incandescence1R, float $incandescence1G, float $incandescence1B
  73. )
  74. {
  75.     float $tubes[];
  76.     float $along[3], $nAlong[3], $normal[3], $across[3], $pnt[3], $dir[3];
  77.     float $strokeSegLength, $elev, $azim, $length, $cosAz, $sinAz, $mid, $pu, $pv;
  78.     int   $i, $j, $numSegments;
  79.  
  80.     // We seed the rand function so that the tubes generated do
  81.     // not change every time they are drawn. The passed in
  82.     // seed changes from step to step along the stroke, but
  83.     // it stays constant across multiple redraws.
  84.     seed( $randSeed++ ); 
  85.  
  86.     // determine how many tubes we want to generate this step 
  87.     int $newTubes =  rand(1) * (1.0 + $tubesPerStep);
  88.  
  89.     if( !$newTubes )
  90.     {
  91.         $tubes[0] = 0;
  92.         return( $tubes ); // no tubes to create in this step
  93.     }
  94.  
  95.     // create a vector pointing along the stroke direction
  96.     $along[0] = $strokeX2 - $strokeX1;
  97.     $along[1] = $strokeY2 - $strokeY1;
  98.     $along[2] = $strokeZ2 - $strokeZ1;
  99.  
  100.     // normalize this vector
  101.     $strokeSegLength = sqrt( $along[0] * $along[0] + $along[1]*$along[1] + $along[2]*$along[2] );
  102.     $nAlong[0] = $along[0]/$strokeSegLength;
  103.     $nAlong[1] = $along[1]/$strokeSegLength;
  104.     $nAlong[2] = $along[2]/$strokeSegLength;
  105.  
  106.     $normal[0] = $nX;
  107.     $normal[1] = $nY;
  108.     $normal[2] = $nZ;
  109.  
  110.     // create a vector pointing across the stroke (using a cross product)
  111.     $across[0]  = $nAlong[1] * $normal[2] - $nAlong[2] * $normal[1];
  112.     $across[1]  = $nAlong[2] * $normal[0] - $nAlong[0] * $normal[2];
  113.     $across[2]  = $nAlong[0] * $normal[1] - $nAlong[1] * $normal[0];
  114.  
  115.     // Start with the twist vector = along and rotate by twist value about the
  116.     // normal. Ideally, one may wish to set this vector for each new tube.
  117.     float $sinTwist = sin( $twist );
  118.     float $cosTwist = cos( $twist );
  119.     float $twistX = $nAlong[0] * $cosTwist + $across[0] * $sinTwist;
  120.     float $twistY = $nAlong[1] * $cosTwist + $across[1] * $sinTwist;
  121.     float $twistZ = $nAlong[2] * $cosTwist + $across[2] * $sinTwist;
  122.  
  123.     // We create the tubes by adding the new tube definitions to the
  124.     // array $tubes.
  125.     // The first value in the array is the number of tubes to generate
  126.     $tubes[0] = $newTubes;
  127.  
  128.     for( $i = 0; $i < $newTubes; $i++ )
  129.     {
  130.         $elev = rand(3.14) * $elevationMax;
  131.         $azim = rand(-3.14,3.14) * $azimuthMax;
  132.         $length = rand(1) * $lengthMax; 
  133.         
  134.         // compute new tube start direction
  135.         $sinAz = sin( $azim );
  136.         $cosAz = cos( $azim );
  137.         $sinEl = sin( $elev );
  138.         $cosEl = cos( $elev );
  139.         $dir[0] = $cosAz * $nAlong[0] + $sinAz * $across[0];
  140.         $dir[1] = $cosAz * $nAlong[1] + $sinAz * $across[1];
  141.         $dir[2] = $cosAz * $nAlong[2] + $sinAz * $across[2];
  142.         $dir[0] = $dir[0] * $cosEl + $normal[0] * $sinEl;
  143.         $dir[1] = $dir[1] * $cosEl + $normal[1] * $sinEl;
  144.         $dir[2] = $dir[2] * $cosEl + $normal[2] * $sinEl;
  145.         // pick a random location between the start and
  146.         // end of the stroke segment
  147.         $mid = rand(1);
  148.         $pnt[0] =  $strokeX1 + $along[0] * $mid;
  149.         $pnt[1] =  $strokeY1 + $along[1] * $mid;
  150.         $pnt[2] =  $strokeZ1 + $along[2] * $mid;
  151.  
  152.         // randomize the new position within
  153.         // a region the size of the brush width
  154.         $pu = rand(-1,1) * $width;
  155.         $pv = rand(-1,1) * $width;
  156.         $pnt[0] += $pv * $nAlong[0] + $pu * $across[0];
  157.         $pnt[1] += $pv * $nAlong[1] + $pu * $across[1];
  158.         $pnt[2] += $pv * $nAlong[2] + $pu * $across[2];
  159.  
  160.         if( $lengthMax  > 0)    
  161.         {
  162.             $numSegments = ($maxSegments * $length)/$lengthMax;
  163.             if( $numSegments < 1 )
  164.             {
  165.                 $numSegments = 1;
  166.             }
  167.         }
  168.         else
  169.         {
  170.             $numSegments = 1;
  171.         }
  172.  
  173.         $j = $i * 36; // there are 36 parameters for a tube
  174.  
  175.         // fill out the array tubes for each new tube
  176.         $tubes[$j+1] = 0; // type: 0 branch, 1 twig, 2 leaf, 3 flower
  177.         $tubes[$j+2] = $randSeed++; // seed to use for noise,etc on tube
  178.         $tubes[$j+3] = $maxSegments; // max segments for tubes
  179.         $tubes[$j+4] = $numSegments; // segments for this tube
  180.  
  181.         $tubes[$j+5] = $pnt[0]; // start position of tube( x,y,z)
  182.         $tubes[$j+6] = $pnt[1];
  183.         $tubes[$j+7] = $pnt[2];
  184.  
  185.         $tubes[$j+8] = $length/(float)$numSegments; // length per segment
  186.  
  187.         $tubes[$j+9] = $dir[0]; // start direction of tube( x,y,z ) 
  188.         $tubes[$j+10] = $dir[1];
  189.         $tubes[$j+11] = $dir[2];
  190.  
  191.         $tubes[$j+12] = $tubeWidth1; // width of tube
  192.         // change in width each segment 
  193.         $tubes[$j+13] = ($tubeWidth2-$tubeWidth1)/(float)$numSegments;
  194.  
  195.         $tubes[$j+14] = $twistX;       // twist vector along tube 
  196.         $tubes[$j+15] = $twistY;
  197.         $tubes[$j+16] = $twistZ;
  198.         $tubes[$j+17] = $spiralMax;
  199.         $tubes[$j+18] = 0;              // flatness
  200.  
  201.         $tubes[$j+19] = $color1R;      // start color( r,g,b )
  202.         $tubes[$j+20] = $color1G;    
  203.         $tubes[$j+21] = $color1B;    
  204.  
  205.          // change in color each segment( r,g,b)
  206.         $tubes[$j+22] = ($color2R-$color1R)/(float)$numSegments;    
  207.         $tubes[$j+23] = ($color2G-$color1G)/(float)$numSegments;    
  208.         $tubes[$j+24] = ($color2B-$color1B)/(float)$numSegments;    
  209.  
  210.         $tubes[$j+25] = $transparency1R; // start transparency( r,g,b )
  211.         $tubes[$j+26] = $transparency1G;    
  212.         $tubes[$j+27] = $transparency1B;    
  213.         $tubes[$j+28] = 0;      // change in transparency each segment( r,g,b)
  214.         $tubes[$j+29] = 0;    
  215.         $tubes[$j+30] = 0;    
  216.  
  217.         $tubes[$j+31] = $incandescence1R; // start incandescence( r,g,b )
  218.         $tubes[$j+32] = $incandescence1G;    
  219.         $tubes[$j+33] = $incandescence1B;    
  220.         $tubes[$j+34] = 0;      // change in incandescence each segment( r,g,b)
  221.         $tubes[$j+35] = 0;    
  222.         $tubes[$j+36] = 0;    
  223.     }
  224.     return( $tubes );
  225. }
  226.  
  227.